assert.c
#include"book.h"
//函数定义
void Menu()
{
        system("cls");
        Sleep(5000);
        printf("**************通讯录**************\n");
        printf("***1.添加联系人    2.查找联系人***\n");
        printf("***3.修改联系人    4.删除联系人***\n");
        printf("***5.清空联系人    6.排序联系人***\n");
        printf("***7.打印联系人    0.退出联系人***\n");
}
//通讯录的初始化
void Book_init(struct address_book* book)
{
        book->capacity = MAX_PEOPLE;
        book->cur_size = 0;
        return;
}
//通讯录中联系人的输入
void Book_add(struct address_book* book)
{
        printf("请输入联系人的姓名>>");
        scanf("%s", book->p[book->cur_size].name);
        printf("请输入联系人的年龄>>");
        scanf("%d", &book->p[book->cur_size].age);
        printf("请输入联系人的性别>>");
        scanf("%s", book->p[book->cur_size].sex);
        printf("请输入联系人的电话>>");
        scanf("%s", book->p[book->cur_size].phone);
        printf("请输入联系人的地址>>");
        scanf("%s", book->p[book->cur_size].address);
        book->cur_size += 1;
        return;
}
//查找过渡(存在则返回联系人下标，失败返回-1)
int fine(struct address_book* book)
{
        char name[50] = {0};
        scanf("%s", name);
        for (int i = 0; i < book->cur_size; i++) {
               if (strcmp(book->p[i].name, name) == 0)
                       return i;
        }
        return -1;
}
//查找(存在则进行打印，失败则返回-1)
int Book_fine(struct address_book* book)
{
        printf("请输入要查找的联系人姓名>>");
        int ret = fine(book);
        if (ret == -1) {
               printf("要查找的联系人不存在!\n");
               return -1;
        }
        else
               Print(book, ret);
        return 0;
}
//查找后的打印函数
void Print(struct address_book* book, int i)
{
        printf("%10s\t%5s\t%5s\t%15s\t%20s\n", "姓名", "性别", "年龄", "电话", "地址");
        printf("%10s\t%5s\t%5d\t%15s\t%20s\n", book->p[i].name, book->p[i].sex, book->p[i].age, book->p[i].phone, book->p[i].address);
}
//通讯录遍历打印函数
void Book_print(struct address_book* book)
{
        printf("%10s\t%5s\t%5s\t%15s\t%20s\n", "姓名", "性别", "年龄", "电话", "地址");
        for (int i = 0; i < book->cur_size; i++)
               printf("%10s\t%5s\t%5d\t%15s\t%20s\n", book->p[i].name, book->p[i].sex, book->p[i].age, book->p[i].phone, book->p[i].address);
}
//删除联系人(不存在返回-1，删除成功返回0)
int Book_del(struct address_book* book)
{
        printf("请输入要删除的联系人姓名>>");
        int ret = fine(book);
        if (ret == -1) {
               printf("要删除的联系人不存在!\n");
               return -1;
        }
        for (int i = ret; i < book->cur_size - 1; i++) {
               book->p[i] = book->p[i + 1];
        }
        book->cur_size--;
        printf("删除联系人完毕!\n");  
        return 0;
}
//联系人修改目录
void Mod_Menu()
{
        printf("********* 修改信息选择 *********\n");
        printf("******* 1.姓名    2.性别 *******\n");
        printf("******* 3.年龄    4.电话 *******\n");
        printf("******* 5.地址    0.退出 *******\n");
        return;
}
//修改联系人(联系人不存在返回-1，修改成功返回0)
int Book_mod(struct address_book* book) {
        printf("请输入要修改的联系人姓名>>");
        int ret = fine(book);
        if (ret == -1) {
               printf("要修改的联系人不存在1\n");
               return -1;
        }
        int choice = -1;
        printf("请输入要要修改信息选项>>");
        Mod_Menu();
        scanf("%d", &choice);
        switch (choice) {
        case 1:
               printf("请输入联系人姓名>>");
               scanf("%s", book->p[ret].name);
               break;
        case 2:
               printf("请输入联系人性别>>");
               scanf("%s", book->p[ret].sex);
               break;
        case 3:
               printf("请输入联系人年龄>>");
               scanf("%d", &book->p[ret].age);
               break;
        case 4:
               printf("请输入联系人电话>>");
               scanf("%s", book->p[ret].phone);
               break;
        case 5:
               printf("请输入联系人地址>>");
               scanf("%s", book->p[ret].address);
               break;
        default:
               printf("退出修改");
               break;
        }
        printf("修改完毕!\n");
        return 0;
}
//清空联系人
void Book_clear(struct address_book* book){
        book->cur_size = 0;
        printf("联系人清空成功!\n");
        return;
}
//排序联系人
void Book_sort(struct address_book* book) {
        for (int i = 0; i < book->cur_size - 1; i++){
               for (int j = 0; j < book->cur_size - 1 - i; j++) {
                       if (strcmp(book->p[j].name, book->p[j + 1].name) > 0) {
                              Swap(book->p[j], book->p[j + 1]);
                       }
               }
        }
        return;
}
//交换函数（用于排序中的交换）
void Swap(struct people_info x1, struct people_info x2)
{
        struct people_info tmp = x1;
        x1 = x2;
        x2 = tmp;
        return;
}
